﻿using Abp.Application.Editions;
using Abp.Application.Features;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.EntityHistory;
using Abp.Localization;
using Abp.Notifications;
using Abp.Organizations;
using Abp.UI.Inputs;
using AutoMapper;
using Camc.OutsourceProcession.Auditing.Dto;
using Camc.OutsourceProcession.Authorization.Accounts.Dto;
using Camc.OutsourceProcession.Authorization.Permissions.Dto;
using Camc.OutsourceProcession.Authorization.Roles;
using Camc.OutsourceProcession.Authorization.Roles.Dto;
using Camc.OutsourceProcession.Authorization.Users;
using Camc.OutsourceProcession.Authorization.Users.Dto;
using Camc.OutsourceProcession.Authorization.Users.Importing.Dto;
using Camc.OutsourceProcession.Authorization.Users.Profile.Dto;
using Camc.OutsourceProcession.Chat;
using Camc.OutsourceProcession.Chat.Dto;
using Camc.OutsourceProcession.Customer;
using Camc.OutsourceProcession.Customer.Dto;
using Camc.OutsourceProcession.Editions;
using Camc.OutsourceProcession.Editions.Dto;
using Camc.OutsourceProcession.Friendships;
using Camc.OutsourceProcession.Friendships.Cache;
using Camc.OutsourceProcession.Friendships.Dto;
using Camc.OutsourceProcession.Localization.Dto;
using Camc.OutsourceProcession.MultiTenancy;
using Camc.OutsourceProcession.MultiTenancy.Dto;
using Camc.OutsourceProcession.MultiTenancy.HostDashboard.Dto;
using Camc.OutsourceProcession.MultiTenancy.Payments;
using Camc.OutsourceProcession.MultiTenancy.Payments.Dto;
using Camc.OutsourceProcession.Notifications.Dto;
using Camc.OutsourceProcession.Organizations.Dto;
using Camc.OutsourceProcession.ProduceTask;
using Camc.OutsourceProcession.ProduceTask.Dto;
using Camc.OutsourceProcession.Sessions.Dto;
using System;
using System.Collections.Generic;
using Camc.OutsourceProcession.ManufacturingEquipment;
using Camc.OutsourceProcession.ManufacturingEquipment.Dto;
using Camc.OutsourceProcession.Order;
using Camc.OutsourceProcession.Printer;
using Camc.OutsourceProcession.Printer.Dto;
using Camc.OutsourceProcession.TaskProcess.Dto;
using Camc.OutsourceProcession.TaskProcess.Dto.Worker;
using Camc.OutsourceProcession.TaskProcess.Process;
using Camc.OutsourceProcession.TaskProcess.Station;
using Camc.OutsourceProcession.TaskProcess.TaskLine;
using Camc.OutsourceProcession.TaskProcess.Worker;

namespace Camc.OutsourceProcession
{
    internal static class CustomDtoMapper
    {
        public static void CreateMappings(IMapperConfigurationExpression configuration)
        {
            //Inputs
            configuration.CreateMap<CheckboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<SingleLineStringInputType, FeatureInputTypeDto>();
            configuration.CreateMap<ComboboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<IInputType, FeatureInputTypeDto>()
                .Include<CheckboxInputType, FeatureInputTypeDto>()
                .Include<SingleLineStringInputType, FeatureInputTypeDto>()
                .Include<ComboboxInputType, FeatureInputTypeDto>();
            configuration.CreateMap<StaticLocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>();
            configuration.CreateMap<ILocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>()
                .Include<StaticLocalizableComboboxItemSource, LocalizableComboboxItemSourceDto>();
            configuration.CreateMap<LocalizableComboboxItem, LocalizableComboboxItemDto>();
            configuration.CreateMap<ILocalizableComboboxItem, LocalizableComboboxItemDto>()
                .Include<LocalizableComboboxItem, LocalizableComboboxItemDto>();

            //Chat
            configuration.CreateMap<ChatMessage, ChatMessageDto>();
            configuration.CreateMap<ChatMessage, ChatMessageExportDto>();

            //Feature
            configuration.CreateMap<FlatFeatureSelectDto, Feature>().ReverseMap();
            configuration.CreateMap<Feature, FlatFeatureDto>();

            //Role
            configuration.CreateMap<RoleEditDto, Role>().ReverseMap();
            configuration.CreateMap<Role, RoleListDto>();
            configuration.CreateMap<UserRole, UserListRoleDto>();

            //Edition
            configuration.CreateMap<EditionEditDto, SubscribableEdition>().ReverseMap();
            configuration.CreateMap<EditionCreateDto, SubscribableEdition>();
            configuration.CreateMap<EditionSelectDto, SubscribableEdition>().ReverseMap();
            configuration.CreateMap<SubscribableEdition, EditionInfoDto>();

            configuration.CreateMap<Edition, EditionInfoDto>().Include<SubscribableEdition, EditionInfoDto>();

            configuration.CreateMap<SubscribableEdition, EditionListDto>();
            configuration.CreateMap<Edition, EditionEditDto>();
            configuration.CreateMap<Edition, SubscribableEdition>();
            configuration.CreateMap<Edition, EditionSelectDto>();


            //Payment
            configuration.CreateMap<SubscriptionPaymentDto, SubscriptionPayment>().ReverseMap();
            configuration.CreateMap<SubscriptionPaymentListDto, SubscriptionPayment>().ReverseMap();
            configuration.CreateMap<SubscriptionPayment, SubscriptionPaymentInfoDto>();

            //Permission
            configuration.CreateMap<Permission, FlatPermissionDto>();
            configuration.CreateMap<Permission, FlatPermissionWithLevelDto>();

            //Language
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageEditDto>();
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageListDto>();
            configuration.CreateMap<NotificationDefinition, NotificationSubscriptionWithDisplayNameDto>();
            configuration.CreateMap<ApplicationLanguage, ApplicationLanguageEditDto>()
                .ForMember(ldto => ldto.IsEnabled, options => options.MapFrom(l => !l.IsDisabled));

            //Tenant
            configuration.CreateMap<Tenant, RecentTenant>();
            configuration.CreateMap<Tenant, TenantLoginInfoDto>();
            configuration.CreateMap<Tenant, TenantListDto>();
            configuration.CreateMap<TenantEditDto, Tenant>().ReverseMap();
            configuration.CreateMap<CurrentTenantInfoDto, Tenant>().ReverseMap();

            //User
            configuration.CreateMap<User, UserEditDto>()
                .ForMember(dto => dto.Password, options => options.Ignore())
                .ReverseMap()
                .ForMember(user => user.Password, options => options.Ignore());
            configuration.CreateMap<User, UserLoginInfoDto>();
            configuration.CreateMap<User, UserListDto>();
            configuration.CreateMap<User, ChatUserDto>();
            configuration.CreateMap<User, OrganizationUnitUserListDto>();
            configuration.CreateMap<Role, OrganizationUnitRoleListDto>();
            configuration.CreateMap<CurrentUserProfileEditDto, User>().ReverseMap();
            configuration.CreateMap<UserLoginAttemptDto, UserLoginAttempt>().ReverseMap();
            configuration.CreateMap<ImportUserDto, User>();

            //AuditLog
            configuration.CreateMap<AuditLog, AuditLogListDto>();
            configuration.CreateMap<EntityChange, EntityChangeListDto>();

            //Friendship
            configuration.CreateMap<Friendship, FriendDto>();
            configuration.CreateMap<FriendCacheItem, FriendDto>();

            //OrganizationUnit
            configuration.CreateMap<OrganizationUnit, OrganizationUnitDto>();

			/* ADD YOUR OWN CUSTOM AUTOMAPPER MAPPINGS HERE */
			configuration.CreateMap<Contact, ContactDto>().ReverseMap();
			configuration.CreateMap<ProduceTask.Order, OrderMainOutput>()
				.ForMember(ldto => ldto.Customer, options => options.MapFrom(l => l.Customer.CompanyName))
				.ForMember(ldto => ldto.StateString, options => options.MapFrom(l => Enum.GetName(typeof(OrderState), l.State)))
				.ForMember(ldto => ldto.LogStatistics, options => options.MapFrom(l => GetLastTime(l.ProductLogs).ToShortDateString().ToString() +"("+ l.ProductLogs.Count.ToString() + ")"))
				;
			configuration.CreateMap<ProduceTask.Order, CreateOrUpdateOrderDto>()
				.ForMember(ldto => ldto.Customer, options => options.MapFrom(l => l.Customer.CompanyName));
			

			configuration.CreateMap<SpecialTool, SpecialToolDto>().ReverseMap();
			configuration.CreateMap<ProductLogInput,ProductLog>();
			configuration.CreateMap<ProductLog, ProductLogOutput>();
			
			configuration.CreateMap<ProduceTask.Order, OrderForLaborOutput>().ForMember(ldto => ldto.CustomerName, options => options.MapFrom(l => l.Customer.CompanyName));
			
			configuration.CreateMap<Process, ProcessDto>();


			//导管
			configuration.CreateMap<LineTemplateDto, LineTemplate>();
			configuration.CreateMap<LineTemplate, LineTemplateUpdate>();

			configuration.CreateMap<NodeBaseDto, NodeBase>();
			configuration.CreateMap<NodeBase, NodeBaseUpdateOrOutput>();

			configuration.CreateMap<NodeForTemplateDto, NodeForTemplate>().ReverseMap();

			configuration.CreateMap<WorkStationDto, WorkStation>();
			configuration.CreateMap<WorkStation, WorkStationUpdateOrOutput>();

			configuration.CreateMap<WorkingTeam, WorkingTeamDto>();
			configuration.CreateMap<WorkingTeam, GetAllWorkingTeamInput>();
			configuration.CreateMap<UpdateWorkingTeam,WorkingTeam>();
			configuration.CreateMap<CreateWorkingTeam, WorkingTeam>();


			//外协管理
			configuration.CreateMap<PrintEquipment, PrintEquipmentDto>();
			configuration.CreateMap<UpdatePrintEquipment, PrintEquipment>();
			configuration.CreateMap<CreatePrintEquipment, PrintEquipment>();

			configuration.CreateMap<Equipment, EquipmentDto>();
			configuration.CreateMap<UpdateEquipment, Equipment>();
			configuration.CreateMap<CreateEquipment, Equipment>();
		}

		private static DateTime GetLastTime(ICollection<ProduceTask.ProductLog> ProductLogs)
		{
			DateTime time = new DateTime();
			foreach (var ProductLog in ProductLogs)
			{
				if (ProductLog.CreationTime > time)
				{
					time = ProductLog.CreationTime;
				}
			}
			return time;
		}
    }
}